﻿<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Extension Methods </title>
    <meta name="viewport" content="width=device-width">
    <meta name="title" content="Extension Methods ">
    <meta name="generator" content="docfx 2.45.1.0">
    
    <link rel="shortcut icon" href="../favicon.ico">
    <link rel="stylesheet" href="../styles/docfx.vendor.css">
    <link rel="stylesheet" href="../styles/docfx.css">
    <link rel="stylesheet" href="../styles/main.css">
    <meta property="docfx:navrel" content="">
    <meta property="docfx:tocrel" content="toc.html">
    <meta property="docfx:rel" content="../">
    <meta property="og:title" content="Flexible and asynchronous testing framework for ASP.NET Core MVC">
    <meta property="og:site_name" content="My Tested ASP.NET Core MVC Docs">
    <meta property="og:url" content="http://docs.mytestedasp.net/">
    <meta property="og:description" content="A fluent unit testing library for ASP.NET Core MVC">
    <meta property="og:image" content="https://mytestedasp.net/Content/Images/logosocial.jpg">
    <meta property="og:type" content="website">
    <meta property="og:locale" content="en_US">
    <meta property="twitter:card" content="summary">
    <meta property="twitter:title" content="Flexible and asynchronous testing framework for ASP.NET Core MVC">
    <meta property="twitter:description" content="A fluent unit testing library for ASP.NET Core MVC">
    <meta property="twitter:creator" content="@MyTestedASPNET">
    <meta property="twitter:url" content="https://mytestedasp.net/">
    <meta property="twitter:image" content="https://mytestedasp.net/Content/Images/logosocial.jpg">
  </head>
  <body data-spy="scroll" data-target="#affix" data-offset="120">
    <div id="wrapper">
      <header>
        <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              <a class="navbar-brand" href="../index.html">
                MY TESTED ASP.NET CORE MVC DOCS
              </a>
            </div>
            <div class="collapse navbar-collapse" id="navbar">
              <form class="navbar-form navbar-right" role="search" id="search">
                <div class="form-group">
                  <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
                </div>
              </form>
            </div>
          </div>
        </nav>
        
        <div class="subnav navbar navbar-default">
          <div class="container hide-when-search" id="breadcrumb">
            <ul class="breadcrumb">
              <li></li>
            </ul>
          </div>
        </div>
      </header>
      <div role="main" class="container body-content hide-when-search">
        
        <div class="sidenav hide-when-search">
          <a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
          <div class="sidetoggle collapse" id="sidetoggle">
            <div id="sidetoc"></div>
          </div>
        </div>
        <div class="article row grid-right">
          <div class="col-md-10">
            <article class="content wrap" id="_content" data-uid="">
<h1 id="extension-methods">Extension Methods</h1>

<p>In this final section of the tutorial we will learn how to extend the functionality of My Tested ASP.NET Core MVC.</p>
<h2 id="extending-existing-test-builders">Extending existing test builders</h2>
<p>Let&#39;s add an extension method which allows as to assert collection models like:</p>
<pre><code class="lang-c#">.WithModelOfType&lt;List&lt;Album&gt;&gt;()
.Passing(albums =&gt; albums.Count == 6)
</code></pre><p>But on a single line:</p>
<pre><code class="lang-c#">.WithCollectionModelOfType&lt;Albums&gt;(albums =&gt; albums.Count == 6)
</code></pre><p>If we take a look at the <strong>&quot;WithModelOfType&quot;</strong> signature, we will see it is an extension method to the <strong>&quot;IBaseTestBuilderWithResponseModel&quot;</strong> interface:</p>
<pre><code class="lang-c#">public static IAndModelDetailsTestBuilder&lt;TModel&gt; WithModelOfType&lt;TModel&gt;(this IBaseTestBuilderWithResponseModel builder);
</code></pre><p>We will extend the same interface so that all action results with models are extended. Create a folder <strong>&quot;Extensions&quot;</strong> in the test project and add <strong>&quot;ResponseModelExtensions&quot;</strong> class containing the code below.</p>
<pre><code class="lang-c#">using MyTested.AspNetCore.Mvc.Builders.Base;
using MyTested.AspNetCore.Mvc.Builders.Contracts.Base;
using MyTested.AspNetCore.Mvc.Exceptions;
using MyTested.AspNetCore.Mvc.Utilities;
using MyTested.AspNetCore.Mvc.Utilities.Extensions;
using System;
using System.Collections.Generic;

public static class ResponseModelExtensions
{
    public static IBaseTestBuilderWithComponent WithCollectionModelOfType&lt;TModel&gt;(
        // base test builder we are extending
        this IBaseTestBuilderWithResponseModel builder,
        // optional predicate the model should pass
        Func&lt;ICollection&lt;TModel&gt;, bool&gt; predicate = null)
    {
        // cast to the actual class behind the interface
        var actualBuilder = (BaseTestBuilderWithResponseModel)builder;
        // helper method validating the model type
        var modelCollection = actualBuilder.GetActualModel&lt;ICollection&lt;TModel&gt;&gt;();

        // execute the predicate if exists
        if (predicate != null &amp;&amp; !predicate(modelCollection))
        {
            // get the current test context
            var testContext = actualBuilder.TestContext;

            // throw exception for invalid predicate
            throw new ResponseModelAssertionException(string.Format(
                &quot;When calling {0} in {1} expected response model collection of {2} to pass the given predicate, but it failed.&quot;,
                testContext.MethodName,
                testContext.Component.GetName(),
                typeof(TModel).ToFriendlyTypeName()));
        }

        // return the same test builder
        return actualBuilder;
    }
}
</code></pre><p>Let&#39;s break it down and explain the most important parts of this extension method:</p>
<ul>
<li><strong>&quot;IBaseTestBuilderWithComponent&quot;</strong> is a base interface containing <strong>&quot;ShouldPassFor&quot;</strong> methods allowing you to continue the fluent API.</li>
<li><strong>&quot;actualBuilder&quot;</strong> is a variable holding the actual class behind the <strong>&quot;IBaseTestBuilderWithResponseModel&quot;</strong> interface. The class&#39; name is the same as the interface but without the &#39;I&#39; in front of it. After the casting you will receive more functionality you can use - methods like the <strong>&quot;GetActualModel&quot;</strong> used above. Their purpose is to help you execute the test but not to be part of the actual fluent API.</li>
<li>The <strong>&quot;TestContext&quot;</strong> is part of every single test builder class. It contains information about the currently executed test. For example in the scope of a controller test, the <strong>&quot;Component&quot;</strong> property will contain the controller instance and the <strong>&quot;MethodName&quot;</strong> property will contain the name of the tested action. More information available <a href="/guide/testcontext.html">HERE</a>.</li>
<li>The <strong>&quot;GetName&quot;</strong> and <strong>&quot;ToFriendlyTypeName&quot;</strong> extension methods can be used to format various display names.</li>
</ul>
<h2 id="using-existing-methods">Using existing methods</h2>
<p>Let&#39;s add new testing functionality based on existing methods. For example instead of this call:</p>
<pre><code class="lang-c#">.ShouldHave()
.Attributes(attributes =&gt; attributes
    .SpecifyingArea(&quot;Admin&quot;))
</code></pre><p>We remove the magic string:</p>
<pre><code class="lang-c#">.ShouldHave()
.Attributes(attributes =&gt; attributes
    .SpecifyingAdminArea())
</code></pre><p>We need to extend the <strong>&quot;IControllerActionAttributesTestBuilder<tattributestestbuilder>&quot;</tattributestestbuilder></strong> interface. Add <strong>&quot;ControllerActionAttributeExtensions&quot;</strong> class with the following code in it:</p>
<pre><code class="lang-c#">using MyTested.AspNetCore.Mvc;
using MyTested.AspNetCore.Mvc.Builders.Contracts.Attributes;

public static class ControllerActionAttributeExtensions
{
    public static TAttributesTestBuilder SpecifyingAdminArea&lt;TAttributesTestBuilder&gt;(
        this IControllerActionAttributesTestBuilder&lt;TAttributesTestBuilder&gt; builder)
        where TAttributesTestBuilder : IControllerActionAttributesTestBuilder&lt;TAttributesTestBuilder&gt;
        =&gt; builder.SpecifyingArea(&quot;Admin&quot;);
}
</code></pre><h2 id="final-words">Final words</h2>
<p>With this section we conclude the tutorial successfully! The final source code with all tests is available <a href="https://raw.githubusercontent.com/ivaylokenov/MyTested.AspNetCore.Mvc/master/docs/files/MusicStore-Tutorial-Final.zip">HERE</a>. But before we say goodbye let&#39;s rebuild and rerun all tests again just for the sake of it. Do the same with the CLI by running <strong>&quot;dotnet test&quot;</strong>. Everything passes? Good!</p>
<p>Hopefully, you fell in love with My Tested ASP.NET Core MVC and if not - ideas and suggestions are <a href="https://mytestedasp.net/#contact">always welcome</a>!</p>
<p>Thank you for reading the whole tutorial and have fun testing your web applications! :)</p>
</article>
          </div>
          
          <div class="hidden-sm col-md-2" role="complementary">
            <div class="sideaffix">
              <div class="contribution">
                <ul class="nav">
                  <li>
                    <a href="https://github.com/tmollov/MyTested.AspNetCore.Mvc/blob/development/docs/_docfx/tutorial/extensionmethods.md/#L1" class="contribution-link">Improve this Doc</a>
                  </li>
                </ul>
              </div>
              <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
              <!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
              </nav>
            </div>
          </div>
        </div>
      </div>
      <footer>
        <div class="grad-bottom"></div>
        <div class="footer">
          <div class="container">
            <span class="pull-right">
              <a href="#top">Back to top</a>
            </span>
            <span>Copyright © 2015-2016 <strong><a href="http://mytestedasp.net">MyTestedASP.NET</a></strong>. All Rights Reserved. Generated by <strong><a href="http://dotnet.github.io/docfx/">DocFX</a></strong></span>
          </div>
        </div>
      </footer>
    </div>
    <script type="text/javascript" src="../styles/docfx.vendor.js"></script>
    <script type="text/javascript" src="../styles/docfx.js"></script>
    <script type="text/javascript" src="../styles/main.js"></script>
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-51720358-4', 'auto');
      ga('send', 'pageview');
    </script>
    <script>
    !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
    n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
    n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
    t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
    document,'script','https://connect.facebook.net/en_US/fbevents.js');
    fbq('init', '884740311601716');
    fbq('track', 'PageView');
    </script>
    <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=884740311601716&ev=PageView&noscript=1"></noscript>  </body>
</html>
